//Copyright (c) 2012 The cwkshell Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 
#ifndef CUC_CWKSHELL_BROWSER_URL_REQUEST_SHELL_JOB_H_ 
#define  CUC_CWKSHELL_BROWSER_URL_REQUEST_SHELL_JOB_H_ 
#include "base/memory/ref_counted_memory.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_simple_job.h"
#include "net/base/io_buffer.h"
namespace cuc{ 
	namespace shell{ 

		class ShellWebUIDataManager;
	
		class URLRequestShellJob : public net::URLRequestJob,
			public base::SupportsWeakPtr<URLRequestShellJob> {
		public:
		
			URLRequestShellJob(net::URLRequest* request,
				net::NetworkDelegate* network_delegate
				,ShellWebUIDataManager* shell_webui_data_manager);
		
			 // net::URLRequestJob implementation.
			 virtual void Start() OVERRIDE;
			 virtual void Kill() OVERRIDE;
			 virtual bool ReadRawData(net::IOBuffer* buf,
				 int buf_size,
				 int* bytes_read) OVERRIDE;
		

			virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
		
			virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;

			// Used to notify that the requested data's |mime_type| is ready.
			void MimeTypeAvailable(const std::string& mime_type);

			// Called by ChromeURLDataManager to notify us that the data blob is ready
			// for us.
			void DataAvailable(base::RefCountedMemory* bytes);
			
			void set_mime_type(const std::string& mime_type) {
				mime_type_ = mime_type;
			}
			void set_allow_caching(bool allow_caching) {
				allow_caching_ = allow_caching;
			}
		private:
			virtual ~URLRequestShellJob();


			// Helper for Start(), to let us start asynchronously.
			// (This pattern is shared by most net::URLRequestJob implementations.)
			void StartAsync();

			// Do the actual copy from data_ (the data we're serving) into |buf|.
			// Separate from ReadRawData so we can handle async I/O.
			void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);

			// The actual data we're serving.  NULL until it's been fetched.
			scoped_refptr<base::RefCountedMemory> data_;
			// The current offset into the data that we're handing off to our
			// callers via the Read interfaces.
			int data_offset_;

			// For async reads, we keep around a pointer to the buffer that
			// we're reading into.
			scoped_refptr<net::IOBuffer> pending_buf_;
			int pending_buf_size_;
			std::string mime_type_;

			// If true, set a header in the response to prevent it from being cached.
			bool allow_caching_;

			 base::WeakPtrFactory<URLRequestShellJob> weak_factory_;
			
			 ShellWebUIDataManager* shell_webui_data_manager_;
			
			DISALLOW_COPY_AND_ASSIGN(URLRequestShellJob);
		};

	} //namespace shell 
} //namespace cuc 
#endif //CUC_CWKSHELL_BROWSER_URL_REQUEST_SHELL_JOB_H_ 
